using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace TCC.Framework
{
    public class OnAssetsEvent : UnityEditor.AssetModificationProcessor
    {
        private static ProtectFileList protectFileList;

        public static ProtectFileList ProtectFileList
        {
            get
            {
                return protectFileList=protectFileList ?? AssetDatabase.LoadAssetAtPath<ProtectFileList>(EditorConfigFile.protectAssetListPath);
            }
        }

        /// <summary>
        /// 资源删除事件
        /// </summary>
        public static TAction<int> DeleteAssetEvent;

        /// <summary>
        /// 资源移动事件
        /// </summary>
        public static TAction<int> MoveAssetEvent;

        public static TAction<string> CreateAssetEvent;

        [InitializeOnLoadMethod]
        static void EditorApplication_projectChanged()
        {
            //--全局监听Project视图下的资源是否发生变化（添加 删除 移动等）
            EditorApplication.projectChanged += delegate ()
            {
                //Debug.Log("资源状态发生变化！");
            };
        }
        //--监听“双击鼠标左键，打开资源”事件
        public static bool IsOpenForEdit(string assetPath, out string message)
        {
            message = null;
            return true;
        }
        //--监听“资源即将被创建”事件
        public static void OnWillCreateAsset(string path)
        {
            CreateAssetEvent?.Invoke(path);
        }
        //--监听“资源即将被保存”事件
        public static string[] OnWillSaveAssets(string[] paths)
        {
            if (paths != null)
            {
                //Debug.Log("资源即将被保存 path :" + string.Join(",", paths));
            }
            return paths;
        }
        //--监听“资源即将被移动”事件
        public static AssetMoveResult OnWillMoveAsset(string oldPath, string newPath)
        {
            int id = GetInstanceID(oldPath);
            MoveAssetEvent?.Invoke(id);
            if (IsExsitAsset(id, ProtectType.Move))
            {
                Log.Error($"该资源【{oldPath}】受保护，不能被移动！");
                return AssetMoveResult.FailedMove;
            }
            return AssetMoveResult.DidNotMove;
        }

        /// <summary>
        /// --监听 资源即将被删除事件
        /// </summary>
        /// <param name="assetPath"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions option)
        {
            int id = GetInstanceID(assetPath);
            DeleteAssetEvent?.Invoke(id);
            if (IsExsitAsset(id,ProtectType.Delete))
            {
                Log.Error($"该资源【{assetPath}】受保护，不能被删除！");
                return AssetDeleteResult.FailedDelete;
            }
            return AssetDeleteResult.DidNotDelete;
        }

        /// <summary>
        /// 资源是否受保护
        /// </summary>
        /// <param name="instanceID">资源ID</param>
        /// <param name="protectType">受保护类型</param>
        /// <returns></returns>
        static bool IsExsitAsset(int instanceID,ProtectType protectType)
        {
            ProtectFileList.Data data = ProtectFileList.datas.Find((i) => { return i.asset.GetInstanceID() == instanceID; });

            return data != null && (protectType == ProtectType.Move ? data.isNoMove : data.isNoDelete);
        }

        /// <summary>
        /// 获取资源ID
        /// </summary>
        /// <param name="assetPath">资源路径</param>
        /// <returns></returns>
        static int GetInstanceID(string assetPath)
        {
            Object obj = AssetDatabase.LoadMainAssetAtPath(assetPath);
            return obj == null ? -1 : obj.GetInstanceID();
        }
        /// <summary>
        /// 资源保护类型
        /// </summary>
        public enum ProtectType
        {
            /// <summary>
            /// 删除
            /// </summary>
            Delete,
            /// <summary>
            /// 移动
            /// </summary>
            Move,
        }
    }
}
